/************************************
 * Package: RGcpp					*
 * Authors: Rui Eduardo Gouveia Gil	*
 * Date: 2010						*
 ************************************/
#ifndef __RGCPP_JOBSCHEDULER_H__
#define __RGCPP_JOBSCHEDULER_H__

#include <boost/thread.hpp>

#include "rgcpp/sys/Job.h"
#include "rgcpp/structs/Map.h"
#include "rgcpp/sys/JobQueue.h"
#include "rgcpp/utils/LogHandler.h"
#include "rgcpp/structs/Sequence.h"
#include "rgcpp/exceptions/RuntimeException.h"

namespace rgcpp
{

class JobScheduler
{
	friend class Job;

	private:
		bool running;
		boost::mutex mut;
		unsigned int num;
		unsigned int exeNum;
		JobQueue waitingQueue;
		boost::thread** mThreads;
		boost::condition_variable cond;
		Sequence<unsigned int> jobSequenceId;
		Map<unsigned int, boost::shared_ptr<Job> >jobMap;

	public:
		JobScheduler(unsigned int threadNum);
		~JobScheduler();

		unsigned int scheduleJob(boost::shared_ptr<Job> job);
		void rescheduleJob(unsigned int jobId);
		boost::shared_ptr<Job> getJob(unsigned int jobId);
		void removeJob(unsigned int jobId);

		void operator()();

	private:
		void scheduleChildJob(unsigned int parentId, boost::shared_ptr<Job> job);

};

}

#endif
